import keyring
import os
import xml.etree.ElementTree as ET

# 指定文件夹路径 参考 /Users/用户名/Library/Application Support/JetBrains/DataGrip版本/options/dataSources
folder_path = '/Users/用户名/Library/Application Support/JetBrains/DataGrip版本/options/dataSources'

# 遍历文件夹下的所有文件
for filename in os.listdir(folder_path):
    if filename.endswith('.xml'):
        file_path = os.path.join(folder_path, filename)
        # 解析XML文件
        tree = ET.parse(file_path)
        root = tree.getroot()
        # 查找dataSource的name参数
        for element in root.iter('dataSource'):
            name = element.get('name')
            # 通过名称获取登录凭据
            credential = keyring.get_credential('IntelliJ Platform DB — ' + os.path.splitext(filename)[0], '')
            # 打印密码
            print(f"文件名: {filename}, 账户名称: {name} 密码: {credential.password}")